import homeAPI from "@/api/index.js"
const state={
	navListData:[],
	bannerListData:[],
	iconListData:[],
	activeNewData:{},
	hotBuyGoodsListData:{},
	factoryGoodsListData:[],
	factoryGoodsItemData:[]
}
const mutations={
	SET_NAVLISTDATA(state,navList){
		state.navListData = navList
	},
	SET_BANNERLIST(state,bannerList){
		state.bannerListData = bannerList
	},
	SET_ICONLISTDATA(state,iconList){
		state.iconListData = iconList
	},
	SET_ACTIVENEWDATA(state,activeNew){
		state.activeNewData = activeNew
	},
	SET_HOTBUYGOODSLISTDATA(state,hotBuyGoodsList){
		state.hotBuyGoodsListData = hotBuyGoodsList
	},
	SET_FACTORYGOODSLIST(state,factoryGoodsList){
		state.factoryGoodsListData = factoryGoodsList
	},
	SET_FACTORYGOODSITEM(state,factoryGoodsItem){
		state.factoryGoodsItemData = factoryGoodsItem
	},
}
const actions = {
	async getNavListData({commit}){
		try{
			const result = await homeAPI.reqhomeHeaderNavData();
			commit('SET_NAVLISTDATA',result.data.list.map((item,index)=>{
				return {
					id:index,
					title:item.title
				}
			}))
			return 'ok'
		}catch(e){
			return rejects(new Error('failed'))
		}
	},
	async getBannerListData({commit}){
		try{
			const result = await homeAPI.reqBannerListData();
			// console.log(result.data[1].map((item,index)=>{
			// 	return {
			// 		id:item.id,
			// 		index,
			// 		img:item.mImage
			// 	}
			// }))
			commit('SET_BANNERLIST',result.data[1].map((item,index)=>{
				return {
					id:item.id,
					index,
					img:item.mImage
				}
			}))
			return 'ok'
		}catch(e){
			return rejects(new Error('failed'))
		}
	},
	async getIconListData({commit}){
		try{
			const result = await homeAPI.reqIconListData();
			// console.log(result.data[1].map((item,index)=>{
			// 	return {
			// 		id:item.id,
			// 		index,
			// 		img:item.mImage
			// 	}
			// }))
			commit('SET_ICONLISTDATA',result.data.functionIcons.map((item,index)=>{
				return {
					id:item.id,
					iconUrl:item.icon,
					title:item.title
				}
			}))
			return 'ok'
		}catch(e){
			return rejects(new Error('failed'))
		}
	},
	async getActiveNewData({commit}){
		try{
			const result = await homeAPI.reqHomeActiveNew()
			commit('SET_ACTIVENEWDATA',result.data)
			return 'ok'
		}catch(e){
			return rejects(new Error('failed'))
		}
	},
	async getHotBuyGoodsList({commit}){
		try{
			const result = await homeAPI.reqHotBuyGoodsList()
			commit('SET_HOTBUYGOODSLISTDATA',result.data)
			return 'ok'
		}catch(e){
			return rejects(new Error('failed'))
		}
	},
	async getFactoryGoodsList({commit}){
		try{
			const result = await homeAPI.reqFactoryGoodsList()
			commit('SET_FACTORYGOODSLIST',result.data)
			return 'ok'
		}catch(e){
			return rejects(new Error('failed'))
		}
	},
	async getFactoryGoodsItem({commit},factoryId){
		try{
			const result = await homeAPI.reqFactoryGoodsItem(factoryId)
			commit('SET_FACTORYGOODSITEM',result.data)
			return 'ok'
		}catch(e){
			return rejects(new Error('failed'))
		}
	},
}
const getters = {}
export default{
	namespaced:true,
	state,
	mutations,
	actions,
	getters
}